
function checkRegisterForm() {
	var reg_mail = /^[A-Za-z0-9]+([_\.\-]?[A-Za-z0-9])*@[A-Za-z0-9]+([\.\-]?[A-Za-z0-9]+)*(\.[A-Za-z]+)+$/;
    var check =  document.getElementById('username');
    var result = true;
    if (check.value == "") {
        document.getElementById('errorForm').innerHTML = "Please enter username";
        result = false;
    }
    else if (check.value.length < 6) {
    	document.getElementById('errorForm').innerHTML = "Minimum username length is 6";
    	result = false;
    }
    else {
        check = document.getElementById('password');
        if (check.value == "") {
    	   document.getElementById('errorForm').innerHTML = "Please enter your password";
    	   result = false;
        }
        else if (check.value.length < 6) {
    	   document.getElementById('errorForm').innerHTML = "Minimum password length is 6";
    	   result = false;
        }
        else {
            check = document.getElementById('repassword');
            if (check.value == "") {
    	       document.getElementById('errorForm').innerHTML = "Please confirm your password";
    	       result = false;
            }
            else {
                var password =  document.getElementById('password');
                if (check.value != password.value) {
                    document.getElementById('errorForm').innerHTML = "Password not match";
                    result = false;
                }
                else {
                    check = document.getElementById('email');
                    if (check.value == "") {
    	               document.getElementById('errorForm').innerHTML = "Please enter your email";
    	               result = false;
                    }
                    else if (reg_mail.test (check.value) == false) {
    	               document.getElementById('errorForm').innerHTML = "Incorrect email format";
    	               result = false;
                    }
                    else document.getElementById('errorForm').innerHTML = "";
                }
            }
        }
    }
    return result;
}

function usernameDuplicate () {
    document.getElementById('errorForm').innerHTML = "Username is not available";
}

function loginFail () {
    document.getElementById('loginFail').innerHTML = "Username or password incorrect!";
}

function checkLoginForm () {
    var check =  document.getElementById('username');
    var result = true;
    if (check.value == "") {
        document.getElementById('loginFail').innerHTML = "Please enter username";
        result = false;
    }
    else if (check.value.length < 6) {
        document.getElementById('loginFail').innerHTML = "Username invalid";
        result = false;
    }
    else {
        check = document.getElementById('password');
        if (check.value == "") {
           document.getElementById('loginFail').innerHTML = "Please enter your password";
           result = false;
        }
        else if (check.value.length < 6) {
           document.getElementById('loginFail').innerHTML = "Password invalid";
           result = false;
        }
        else document.getElementById('errorForm').innerHTML = "";
    }
    return result;
}

function checkUpdateForm () {
    var reg_mail = /^[A-Za-z0-9]+([_\.\-]?[A-Za-z0-9])*@[A-Za-z0-9]+([\.\-]?[A-Za-z0-9]+)*(\.[A-Za-z]+)+$/;
    var check =  document.getElementById('email');
    var result = true;
    if (check.value != "") {
        if (reg_mail.test (check.value) == false) {
            document.getElementById('emailError').innerHTML = "Incorrect email format";
            result = false;
        }
        document.getElementById('emailError').innerHTML = "";
    }
    else document.getElementById('emailError').innerHTML = "";

    check =  document.getElementById('phone');
    if (check.value != "") {
        if (check.value.length < 10 || check.value.length > 12) {
            document.getElementById('phoneError').innerHTML = "Incorrect phone number format";
            result = false;
        }
        else document.getElementById('phoneError').innerHTML = "";
    }
    
    var curPass = document.getElementById('curPass');
    var newPass = document.getElementById('newPass');
    var reNewPass = document.getElementById('reNewPass');

    if (curPass != null && newPass != null && reNewPass != null) {
        if (!(curPass.value==""&&newPass.value==""&&reNewPass.value=="")) {
            if (curPass.value==""||newPass.value==""||reNewPass.value=="") {
                document.getElementById('passError').innerHTML = "Please fill three password fields";
                result = false;
            }
            else if (newPass.value.length < 6) {
                document.getElementById('passError').innerHTML = "Minimum password length is 6";
                result = false;
            }
            else if (newPass.value != reNewPass.value) {
                document.getElementById('passError').innerHTML = "Password not match";
                result = false;
            }
            else document.getElementById('passError').innerHTML = "";
        }
    }

    return result;
}

function func (id, type) {
    var insert = document.getElementById(id);

    if (type = "password") {
        insert.innerHTML="";
        insert.innerHTML+="Current password"
        insert.innerHTML+='<input type="password" id="curPass" class="text_box"><br/><br/>';
        insert.innerHTML+="New password"
        insert.innerHTML+='<input type="password" id="newPass" class="text_box"><br/><br/>';
        insert.innerHTML+="Retype Password"
        insert.innerHTML+='<input type="password" id="reNewPass" class="text_box"><br/>';
    }
} 